package com.cungmua.app.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.cungmua.app.R;
import com.cungmua.app.base.BaseAuthenticatedActivity;
import com.cungmua.app.handler.ForgotPasswordHandler;
import com.cungmua.app.model.Cart;
import com.cungmua.app.model.request.ForgotPasswordRequest;
import com.cungmua.app.model.response.ForgotPasswordResponse;
import com.cungmua.app.task.ForgotPasswordTask;

/**
 * Created by kelvin.nguyen on 7/25/14.
 */
public class ForgotPasswordActivity extends BaseAuthenticatedActivity implements ForgotPasswordHandler{
    private EditText editTextEmail;
    private ImageButton btnSend;
    private ImageView icon_home;
    private Button icon_cart;
    private LinearLayout btnCart;

    @Override
    public int getLayoutResource() {
        return R.layout.activity_forgot_password;
    }

    @Override
    public void loadControls(Bundle savedInstanceState) {
        editTextEmail=(EditText)findViewById(R.id.editTextEmail);
        btnSend=(ImageButton)findViewById(R.id.btnSend);


        icon_home=(ImageView)findViewById(R.id.icon_home);
        icon_cart=(Button)findViewById(R.id.icon_cart);
        btnCart=(LinearLayout)findViewById(R.id.btnCart);


        Cart currentCart=getCurrentCart();
        icon_cart.setText(currentCart.getTotalQuantity()+"");

        icon_home.setOnClickListener(new ImageView.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMenu();
            }
        });

        btnCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToYourCart();
            }
        });

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 if(!validateFields())
                     return;
                ForgotPasswordActivity activity=ForgotPasswordActivity.this;
                ForgotPasswordRequest request=new ForgotPasswordRequest(getDeviceId(),getToken());
                request.setEmail(editTextEmail.getText().toString());
                ForgotPasswordTask task=new ForgotPasswordTask(activity,activity);
                task.execute(request);
            }
        });


    }

    private boolean validateFields() {
        if (editTextEmail.getText().toString().isEmpty()) {
            alert(getString(R.string.label_forgot_password));
            return false;
        }
        return  true;
    }

    @Override
    public void updateView() {


    }

    public void alert(String alert) {
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setMessage(alert)
                .setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .show();
    }

    @Override
    public void onForgotPassword(ForgotPasswordResponse response) {
        if (response.getCode() == 0) {
            Toast.makeText(this,response.getMessage(),Toast.LENGTH_LONG).show();
        }else{
            alert(response.getMessage());
        }
    }
}
